<?php
namespace AuctionCore\FoursStore;

/**
* 4s店
* @author      
*/
class Fours 
{

	public function __construct() {

    }

    //获取4s店列表
    public function get_fours_list($page, $limit, $params){
        $page = (int)$page<1 ? 1 : (int)$page;
        $limit = (int)$limit<1 ? 10 : (int)$limit;
        //搜索 
        $map = array();
        //查询条件
        if(!empty($params['search_key'])){
            $map['u.user_name'] = array('like','%' .$params['search_key'].'%');
            $map['fu.real_name'] = array('like','%' .$params['search_key'].'%');
            $map['fu.phone'] = array('like','%' .$params['search_key'].'%');
            $map['f.f_name'] = array('like','%' .$params['search_key'].'%');
            $map['_logic'] = 'or';
        }
        //要查询的列
        $cols = "u.user_name,fu.real_name,u.status,fu.phone,u.uid,fu.fu_id,f.f_name";
        $prefix = C("DB_PREFIX");
        $total = M('user')->alias('u')         
            ->join("inner JOIN {$prefix}fours_user as fu ON fu.u_id = u.uid")
            ->join("LEFT JOIN {$prefix}fours f ON f.fid= fu.f_id")
            ->where($map)->count();
        $list = M('user')->alias('u')->field($cols)
            ->join("inner JOIN {$prefix}fours_user as fu ON fu.u_id = u.uid")
            ->join("LEFT JOIN {$prefix}fours f ON f.fid= fu.f_id")
            ->where($map)->limit($limit)->page($page)->select();
            // echo M()->getLastSql();
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;        
        
    }

    //获取集团列表
    public function get_bloc_list($page, $limit, $params){
        $page = (int)$page<1 ? 1 : (int)$page;
        $limit = (int)$limit<1 ? 10 : (int)$limit;
        //搜索 
        $map = array();
        //查询条件
        if(!empty($params['search_key'])){
            $map['u.user_name'] = array('like','%' .$params['search_key'].'%');
            $map['bu.real_name'] = array('like','%' .$params['search_key'].'%');
            $map['bu.phone'] = array('like','%' .$params['search_key'].'%');
            $map['b.b_name'] = array('like','%' .$params['search_key'].'%');
            $map['_logic'] = 'or';
        }
        
        //要查询的列
        $cols = "u.uid,u.user_name,u.status,bu.real_name,bu.phone,bu.bu_id,b.b_name";
        
        $prefix = C("DB_PREFIX");
        $total = M('user')->alias('u')->field($cols)
            ->join("inner JOIN {$prefix}bloc_user as bu on u.uid = bu.u_id ")
            ->join("LEFT JOIN {$prefix}bloc as b ON b.bid = bu.b_id")
            ->where($map)->count();
        $list = M('user')->alias('u')->field($cols)
            ->join("inner JOIN {$prefix}bloc_user as bu on u.uid = bu.u_id ")
            ->join("LEFT JOIN {$prefix}bloc as b ON b.bid = bu.b_id")
            ->where($map)->limit($limit)->page($page)->select();
             // echo M()->getLastSql();
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;        
        
    }

     /**
    *获取所有4S店信息
    **/
    public function get_allfours(){

        $allinfo = M('fours')->field('fid,f_name')->select();

        return  $allinfo;
    }

    /**
    *获取所有集团信息
    **/
    public function get_allbloc(){

        $allinfo = M('bloc')->field('bid,b_name')->select();

        return  $allinfo;
    }
    /**
    *查找4s店账号信息
    **/
    public function get_one_fours($id){
        $prefix = C("DB_PREFIX");
        $cols = "u.user_name,fu.real_name,fu.phone,u.uid,f.fid,f.f_name,f.addr as faddr";
        $list = M('user')->alias('u')->field($cols)
            ->join("inner JOIN {$prefix}fours_user as fu ON fu.u_id = u.uid")
            ->join("LEFT JOIN {$prefix}fours f ON f.fid= fu.f_id")
            ->where('u.uid ='.$id)->find();
        return $list;
    }

    /**
    *查找指定集团账号信息
    **/
    public function get_one_bloc($uid){
        $prefix = C("DB_PREFIX");
        $cols = "u.user_name,bu.real_name,bu.phone,u.uid,b.b_name,b.addr,b.bid";
        $list = M('user')->alias('u')->field($cols)
        ->join("inner JOIN {$prefix}bloc_user as bu ON bu.u_id = u.uid")
        ->join("LEFT JOIN {$prefix}bloc as b ON b.bid = bu.b_id")
        ->where('u.uid ='.$uid)->find();
        return $list;
    }

    /**
    *查找指定集团下属所有4s店信息
    **/
    // public function get_bloc_fours($b_id){
    //     $prefix = C("DB_PREFIX");
    //     $cols = "u.user_name,u.real_name,u.pwd_mobile,f.f_name,f.addr";
    //      $list = M('bloc')->alias('b')->field($cols)
    //         ->join("inner JOIN {$prefix}fours as f ON f.b_id = b.bid")
    //         ->join("inner JOIN {$prefix}fours_user as fu ON f.fid = fu.f_id")
    //         ->join("inner JOIN {$prefix}user as u ON u.uid = fu.u_id")
    //         ->where('b.bid ='.$b_id)->where($where)->select();
    //     return $list;
    // }


     /**
     * 添加4S账号信息
     */
    public static function create($user,$fu)
    {
        //开启事务
        M()->startTrans();
        //添加用户
        $uid = M('user')->add($user);
        //添加到4s组
        $ugd = M('auth_group_access')->add(array('uid' => $uid, 'group_id' => 4));
        //添加4s店对应用户记录
        $fu['u_id'] =  $uid;
        $uf_id = M('fours_user')->add($fu);
        //出错回滚
        if ($uid < 1 || $ugd < 1 || $uf_id <1) {
            M()->rollback();
            return false;
        }
        M()->commit();

        return $uid;
    }

    /**
     * 添加集团账号信息
     */
    public static function createBloc($user,$bu)
    {
        //开启事务
        M()->startTrans();
        //添加用户
        $uid = M('user')->add($user);
        //添加到集团组
        $ugd = M('auth_group_access')->add(array('uid' => $uid, 'group_id' => 5));
        //添加集团对应用户记录
        $bu['u_id'] = $uid;
        $bu_id = M('bloc_user')->add($bu);
        //出错回滚
        if ($uid < 1 || $ugd < 1 || $bu_id < 1 ) {
            M()->rollback();
            return false;
        }
        M()->commit();

        return $uid;
    }

    /**
    * 账号启用分组
    */
    public function enable($uid)
    {
         $ok = M('user')->where(array('uid'=>$uid))->save(array('status'=>'0'));
    }
    
    /**
    * 4s店账号禁用分组
    */
    public function disable($uid)
    {
          $ok = M('user')->where(array('uid'=>$uid))->save(array('status'=>'1'));
    }

    /**
     * 添加4S信息
     */
    // public static function createBf($post_fb,$b_id)
    // {
    //     foreach ($post_fb as $key => $value) {
    //         $map = array();
    //         $map['b_id'] = $b_id;
    //         $map['modifytime'] = date('Y-m-d H:i:s');
    //         $ok = M('fours')->where("id = '$value'")->save($map);
    //     }
    //     return $ok;
    // }


    /**
     * 修改4S信息
     */
    public static function update($user,$fu)
    {
        $uid = array_shift($user);
        //开启事务
        M()->startTrans();
       //修改用户
        $ok = M('user')->where("uid = '$uid'")->save($user);
        //修改用户对应4s店表
        $ok1 = M('fours_user')->where("u_id = '$uid'")->save($fu);
        //出错回滚
        if ($ok < 1 || $ok1 < 1 ) {

            M()->rollback();

            return false;
        }
        M()->commit();

        return $ok;
    }

        /**
     * 修改集团信息
     */
    public static function updateBloc($user,$bu)
    {
        $uid = array_shift($user);
        //开启事务
        M()->startTrans();
        //修改用户
        $ok = M('user')->where(array('uid'=>$uid))->save($user);
        //修改用户对应集团表
        $ok1 = M('bloc_user')->where(array('u_id'=>$uid))->save($bu);
        //出错回滚
        if ($ok < 1 || $ok1 < 1) {
            M()->rollback();
            return false;
        }
        M()->commit();
        return $ok;
    }


    public function restpwd($uid){
       return $ok = M('user')->where(array('uid'=>$uid))->save(array('passwd'=>md5('123456')));
    }

    /***********************************************集团及4S店管理**********************************************************/

    public function get_bmanger_list($page, $limit, $params){
        $page = (int)$page<1 ? 1 : (int)$page;
        $limit = (int)$limit<1 ? 10 : (int)$limit;
        //搜索 
        $map = array();
        //查询条件
        if(!empty($params['search_key'])){
            $map['b_name'] = array('like','%' .$params['search_key'].'%');
            $map['addr'] = array('like','%' .$params['search_key'].'%');
            $map['contact'] = array('like','%' .$params['search_key'].'%');
            $map['phone'] = array('like','%' .$params['search_key'].'%');
            $map['_logic'] = 'or';
        }        
        $total = M('bloc')->where($map)->count();
        $list = M('bloc')->where($map)->limit($limit)->page($page)->select();
        // echo M()->getLastSql();
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;        
        
    }


    public function get_fmanger_list($page, $limit, $params){
        $page = (int)$page<1 ? 1 : (int)$page;
        $limit = (int)$limit<1 ? 10 : (int)$limit;
        //搜索 
        $map = array();
        $where = array();
        //集团管理-下属4s店 传过来的bid
        if ($params['bid']) {
           $where['b.bid'] = array('eq',$params['bid']);
        }
        //查询条件
        if(!empty($params['search_key'])){
            $map['f.f_name'] = array('like','%' .$params['search_key'].'%');
            $map['f.addr'] = array('like','%' .$params['search_key'].'%');
            $map['f.contact'] = array('like','%' .$params['search_key'].'%');
            $map['f.phone'] = array('like','%' .$params['search_key'].'%');
            $map['f.developer'] = array('like','%' .$params['search_key'].'%');
            $map['b.b_name'] = array('like','%' .$params['search_key'].'%');
            $map['_logic'] = 'or';
            $where['_complex'] = $map;
        }
        //要查询的列
        $cols = "f.fid,f.f_name,f.addr,f.contact,f.phone,f.developer,b.b_name";
        
        $prefix = C("DB_PREFIX");
        $total = M('fours')->alias('f')         
            ->join("LEFT JOIN {$prefix}bloc as b ON f.b_id = b.bid")
            ->where($where)->count();
        $list = M('fours')->alias('f')->field($cols)
            ->join("LEFT JOIN {$prefix}bloc as b ON f.b_id = b.bid")
            ->where($where)->limit($limit)->page($page)->select();
            // echo M()->getLastSql();
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;        
        
    }

    public function createbm($bloc){

        //添加
        $bid = M('bloc')->add($bloc);

        return $bid;
    }

    public function createfm($fours,$come_from){

        //开启事务
        M()->startTrans();
        //添加渠道
        $c_id = M('come_from')->add($come_from);

        $fours['channel_id'] = $c_id;
        //添加4s店
        $f_id = M('fours')->add($fours);
        //出错回滚
        if ($c_id < 1 || $f_id < 1) {

            M()->rollback();

            return false;
        }
        M()->commit();

        return $f_id;
    }


    public function find_bloc($bid){
        return M('bloc')->where(array('bid'=>$bid))->find();
    }

    public function find_fours($fid){
        $cols = "f.fid,f.f_name,f.addr,f.contact,f.phone,f.developer,f.channel_id,b.b_name,b.bid,f.city,f.bank_name,
        f.card_no,f.account_name";
        $prefix = C("DB_PREFIX");
        $list = M('fours')->alias('f')->field($cols)
        ->join("LEFT JOIN {$prefix}bloc as b ON f.b_id = b.bid")
        ->where(array('fid'=>$fid))->find();
        return $list;
    }

    public function updatebm($bloc,$bid){

       return $ok  = M('bloc')->where("bid = '$bid'")->save($bloc);

    }

    public function updatefm($fours,$fid){
        //开启事务
        M()->startTrans();
        //修改4s店
        $f_id = M('fours')->where("fid = '$fid'")->save($fours);
        //修改渠道
        $c_id = M('come_from')->where("from_id = '$fours[channel_id]'")->save(array('from_name'=>$fours['f_name']));
      
        if ($f_id < 1) {

            M()->rollback();

            return false;
        }
        M()->commit();

        return $f_id;
    }

    /**
    *   获取指定集团已绑定的4s店id
    **/
    public function get_spm($bid){
        return M('fours')->field('fid')->where("b_id='$bid'")->select();
    }

    public function set_spm($bid,$fid) {
         $ok = M('fours')->where("fid='$fid'")->save(array('b_id'=>$bid));
    }

    public function exgraft_spm($bid,$fid) {
        $ok = M('fours')->where("fid='$fid'")->save(array('b_id'=>0));
    }

    /**
     * 通过渠道ID获取4S店信息
     * @param $channel_id
     * @return bool|mixed
     */
    public static function get_fours_by_channel_id($channel_id)
    {
        if(intval($channel_id)<1) return false;
        return M('fours')->where("channel_id=".intval($channel_id))->find();
    }
}
